import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_data_table.dart';
import 'package:flutter_admin/api/odoo/purchase_order_line_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/purchase_order.dart';
import 'package:flutter_admin/models/odoo/purchase_order_line.dart';

// ignore: must_be_immutable
class PurchaseOrderLineList extends StatefulWidget {
  PurchaseOrder? purchaseOrder;
  PurchaseOrderLineList({required this.purchaseOrder, Key? key})
      : super(key: key);

  @override
  _PurchaseOrderLineListState createState() => _PurchaseOrderLineListState();
}

class _PurchaseOrderLineListState extends State<PurchaseOrderLineList> {
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  PageModel page =
      PageModel(orders: [OrderItemModel(column: 'create_date', asc: false)]);
  PurchaseOrderLine purchaseOrderLine = PurchaseOrderLine();
  List<PurchaseOrderLine> selectedList = [];
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _query();
    });
  }

  @override
  Widget build(BuildContext context) {
    CryDataTable table = CryDataTable(
      key: tableKey,
      title: "数据列表",
      onPageChanged: (firstRowIndex) {
        page.current = (firstRowIndex ~/ page.size + 1);
        _loadData();
      },
      onRowsPerPageChanged: (int size) {
        page.size = size;
        page.current = 1;
        _loadData();
      },
      onSelectChanged: (Map selected) {
        this.setState(() {});
      },
      columns: [
        DataColumn(label: Text('行号')),
        DataColumn(label: Text('下达号')),
        DataColumn(label: Text('物料编码')),
        DataColumn(label: Text('物料名称')),
        DataColumn(label: Text('采购数量')),
        DataColumn(label: Text('单位')),
        DataColumn(label: Text('期望交货日期')),
        DataColumn(label: Text('计划接收日期')),
        DataColumn(label: Text('计划交货日期')),
        DataColumn(label: Text('含税单价(元)')),
        DataColumn(label: Text('含税总价(元)')),
      ],
      getCells: (m) {
        PurchaseOrderLine purchaseOrderLine = PurchaseOrderLine.fromMap(m);
        return [
          DataCell(Text(purchaseOrderLine.lineno ?? '--')),
          DataCell(Text(purchaseOrderLine.releaseno ?? '--')),
          DataCell(Text(purchaseOrderLine.materialcode ?? '--')),
          DataCell(Text(purchaseOrderLine.name ?? '--')),
          DataCell(Text(purchaseOrderLine.productQty ?? '--')),
          DataCell(Text(purchaseOrderLine.productUom ?? '--')),
          // DataCell(Text(Utils.timeUTC(purchaseOrderLine.deliverydate) ?? '--')),
          DataCell(Text(purchaseOrderLine.deliverydate ?? '--')),
          // DataCell(Text(Utils.timeUTC(purchaseOrderLine.receivedate) ?? '--')),
          DataCell(Text(purchaseOrderLine.receivedate ?? '--')),
          // DataCell(Text(
          //     Utils.timeUTC(purchaseOrderLine.planeddeliverydate) ?? '--')),
          DataCell(Text(purchaseOrderLine.planeddeliverydate ?? '--')),
          DataCell(Text(purchaseOrderLine.priceUnit ?? '--')),
          DataCell(Text(purchaseOrderLine.priceSubtotal ?? '--')),
        ];
      },
    );
    var result = Scaffold(
      key: scaffoldStateKey,
      appBar: AppBar(
        leading: Builder(
          builder: (BuildContext context) {
            final ModalRoute<Object?>? parentRoute = ModalRoute.of(context);
            final bool canPop = parentRoute?.canPop ?? false;

            if (canPop) {
              return BackButton();
            } else {
              return SizedBox.shrink();
            }
          },
        ),
        title: Text('订单明细'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 10),
          Expanded(child: SingleChildScrollView(child: table)),
        ],
      ),
    );
    return result;
  }

  _query() {
    // formKey.currentState?.save();
    tableKey.currentState?.pageTo(0);
    _loadData();
  }

  _loadData() async {
    purchaseOrderLine.orderId = widget.purchaseOrder!.id.toString();
    ResponseBodyApi responseBodyApi = await PurchaseOrderLineApi.page(data: {
      "page": page,
      "domainList": purchaseOrderLine.toDomainList(),
    });
    if (responseBodyApi.success!) {
      page = responseBodyApi.data != null ? responseBodyApi.data : PageModel();
      tableKey.currentState!.loadData(page);
    }
  }
}
